﻿using EyFramework.Common;
using EyFramework.Library;
using Ncov2019.Core.Service;
using Ncov2019.Entity;
using Ncov2019.Entity.Dto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ncov2019
{
    public partial class FrmModifyPat : FrmDevBase
    {
        private INcovBarConfirmService ncovBarConfirmService;
        private NcovInfo ncovInfo;

        public FrmModifyPat()
        {
            InitializeComponent();
        }

        public FrmModifyPat(NcovInfo _ncovInfo)
        {
            InitializeComponent();
            ncovInfo = _ncovInfo;
        }

        private void FrmModifyPat_Load(object sender, EventArgs e)
        {
            ncovBarConfirmService = Globals.GetService<INcovBarConfirmService>();
            ShowData();
        }

        private void ShowData()
        {
            lblBrid.Text = ncovInfo.BINGRENID;
            teBrxm.Text = ncovInfo.XINGMING;
            cmbXb.Text = ncovInfo.XB;
            dtCsrq.Text = ncovInfo.CHUSHENGRQ.Value.ToString("yyyy-MM-dd");
            teSfzh.Text = ncovInfo.SHENFENZH;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(teBrxm.Text.Trim()))
            {
                Globals.ShowError("病人姓名不能为空！");
                teBrxm.Focus();
                return;
            }
            if (string.IsNullOrEmpty(teSfzh.Text.Trim()))
            {
                Globals.ShowError("证件号码不能为空！");
                teBrxm.Focus();
                return;
            }
            if (Globals.ShowYesNoAndWarning("本次修改仅修改一周内的核酸和抗体的标本对应的患者信息,是否继续？") == DialogResult.Yes)
            {
                var old = 0;
                var olddw = 0;
                if (int.Parse(DateTime.Now.ToString("yyyy")) - int.Parse(dtCsrq.DateTime.ToString("yyyy")) == 0)
                {
                    if (int.Parse(DateTime.Now.ToString("MM")) - int.Parse(dtCsrq.DateTime.ToString("MM")) == 0)
                    {
                        old = int.Parse(DateTime.Now.ToString("dd")) - int.Parse(dtCsrq.DateTime.ToString("dd"));
                        olddw = 3;
                    }
                    else 
                    {
                        old = int.Parse(DateTime.Now.ToString("MM")) - int.Parse(dtCsrq.DateTime.ToString("MM"));
                        olddw = 2;
                    }
                }
                else {
                    old = int.Parse(DateTime.Now.ToString("yyyy")) - int.Parse(dtCsrq.DateTime.ToString("yyyy"));
                    olddw = 1;
                }
                LisPatInfoDto lisPatInfo = new LisPatInfoDto()
                {
                    BINGRENID = ncovInfo.BINGRENID,
                    XINGMING = teBrxm.Text.Trim(),
                    XINGBIE = (cmbXb.SelectedIndex + 1) + "",
                    CHUSHENGRQ = Convert.ToDateTime(dtCsrq.DateTime),
                    SHENFENZH = teSfzh.Text.Trim(),
                    NIANLING = old,
                    NIANLINGDW = olddw
                };
                if (ncovBarConfirmService.UpdateLisPatInfo(lisPatInfo))
                {
                    Globals.ShowTips("基本信息修改成功！");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }
    }
}